package com.yeban.javase.day09.test;

import java.util.*;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/17
 *
 * //1.模拟登陆，编写用户类，测试类。
 *   1>.给集合中存储5个用户对象。
 *   2>.从控制台输入用户名和密码，和集合中的对象信息进行比较，相同表示成功，不同表示失败。
 *   3>.有三次输入机会。
 */
public class test01 {

    public static void main(String[] args) throws Exception {

        Collection<User> users = new ArrayList<User>();
        users.add(new User("张三","111"));
        users.add(new User("李四","222"));
        users.add(new User("王五","333"));
        users.add(new User("赵六","444"));
        users.add(new User("孙七","555"));

        // User u1 = new User("小明","111");
        int count = 3;
        Scanner sc = new Scanner(System.in);
        try{
            A:while (count > 0) {
                System.out.print("请输入您的账户：");
                String s1 = sc.next();
                System.out.print("请输入您的密码：");
                String s2 = sc.next();
                // 使用contains， contains中也是利用equals实现的，在这里也是需要进行重写的
                if(users.contains(new User(s1,s2))){
                    System.out.println("登录成功！");
                    break A;
                }
                /*for (User user : users) {
                    if(user.equals(new User(s1,s2))) {  // 这里的equals需要重写，否则判断的是地址
                        System.out.println("登录成功！");
                        break A;
                    }
                }*/
                -- count;
            }
        } catch (Exception e) {
            throw new Exception("密码输入错误！");
        }
    }
}

class User {
    String name;
    String passward;
    // ArrayList<User> users;

    public User() {
    }

    public User(String name, String passward) {
        this.name = name;
        this.passward = passward;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(name, user.name) && Objects.equals(passward, user.passward);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, passward);
    }
/*public User(String name, String passward, ArrayList<User> users) {
        this.name = name;
        this.passward = passward;
        this.users = users;
    }*/

    /*public void addUser(User user) {
        users.add(user);
    }*/

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return passward
     */
    public String getPassward() {
        return passward;
    }

    /**
     * 设置
     * @param passward
     */
    public void setPassward(String passward) {
        this.passward = passward;
    }

    public String toString() {
        return "User{name = " + name + ", passward = " + passward + "}";
    }

    /**
     * 获取
     * @return users
     */
    /*public ArrayList<User> getUsers() {
        return users;
    }*/

    /**
     * 设置
     * @param users
     */
    /*public void setUsers(ArrayList<User> users) {
        this.users = users;
    }*/
}
